package com.example.redis.spider;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class UrlGetter {
    private static String URL = "https://blog.csdn.net/api/articles?type=more&category=java&shown_offset=0";
    private static String COOKIE = "uuid_tt_dd=10_19030928420-1593506445304-546229; dc_session_id=10_1593506445304.565314; dc_sid=0af3d02f3cc65185bb82c83bdd4c0afb; TY_SESSION_ID=6da16911-3ac5-4327-b28d-318a1c8273c5; c_first_ref=default; c_first_page=https%3A//blog.csdn.net/nav/java; dc_tos=qcqdfy; c-login-auto=1; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1593506447; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1593506447; Hm_up_6bcd52f51e9b3dce32bec4a3997715ac=%7B%22islogin%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22isonline%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22isvip%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%7D; Hm_ct_6bcd52f51e9b3dce32bec4a3997715ac=6525*1*10_19030928420-1593506445304-546229; announcement=%257B%2522isLogin%2522%253Afalse%252C%2522announcementUrl%2522%253A%2522https%253A%252F%252Fmarketing.csdn.net%252Fp%252F00839b3532e2216b0a7a29e972342d2a%253Futm_source%253D618%2522%252C%2522announcementCount%2522%253A0%252C%2522announcementExpire%2522%253A3600000%257D";

    public static List<String> getUrl() {
        String s = HttpRequest.get(URL)
                .cookie(COOKIE)
                .execute().body();
        JSONObject jo = JSONObject.parseObject(s);
        ArrayList<String> list = CollUtil.newArrayList();
        JSONArray ja = JSONObject.parseArray(jo.getString("articles"));
        for (int i = 0; i < ja.size(); i++) {
            list.add(ja.getJSONObject(i).getString("url"));
            log.info("获取到url：{}",ja.getJSONObject(i).getString("url"));
        }
        return list;
    }
}
